package cn.com.wxd.util.user;

import java.util.HashMap;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.wxd.biz.user.UsersService;
import cn.com.wxd.biz.user.UsersServiceImpl;
import cn.com.wxd.entity.user.UsersInfo;
import cn.com.wxd.service.permissions.core.PermissionsCore;
import cn.com.wxd.service.permissions.core.RoleCore;
import cn.com.wxd.service.permissions.entity.PermissionsEntity;
import cn.com.wxd.service.permissions.entity.RoleEntity;
import cn.com.wxd.util.SpringContextHolder;

public class UserUtil {
    private static final Logger log = LoggerFactory.getLogger(UserUtil.class);

    /**
     * 获取用户数量
     *
     * @return
     */
    public static int getUserCount() {
        UsersService us = SpringContextHolder.getBean("usersService", UsersServiceImpl.class);
        try {
            return us.getUserCount();
        } catch (Exception e) {
            log.error("", e);
            return -1;
        }
    }

    /**
     * 刷新系统级用户权限
     *
     * @return
     */
    public static void initSystemAdminPermissions() {
        UsersService us = SpringContextHolder.getBean("usersService", UsersServiceImpl.class);
        String allRole = getAllRoleCodeStr();
        HashMap<String, String> param = new HashMap<>();
        param.put("userType", "SYSTEM");
        List<UsersInfo> usersInfos = us.queryUsersInfos(param);
        for (UsersInfo usersInfo : usersInfos) {
            usersInfo.setRoles(allRole);
            us.updateRoles(usersInfo.getId(), allRole);
        }
    }

    /**
     * 获取所有的RolesCode使用\2分割开的
     *
     * @return
     */
    private static String getAllRoleCodeStr() {
        try {
            List<RoleEntity> roles = RoleCore.getInstance().getRoles();
            String roleStr = "";
            for (RoleEntity role : roles) {
                if (!role.getCode().trim().equals("")) {
                    roleStr += role.getCode() + "\2";
                }
            }
            return roleStr;
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("获取所有的角色出现错误!", e);
            }
            return "";
        }
    }

    /**
     * 获取所有的Roles
     *
     * @return
     */
    public static List<RoleEntity> getAllRoles() {
        try {
            List<RoleEntity> roles = RoleCore.getInstance().getRoles();
            return roles;
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("获取所有的角色出现错误!", e);
            }
            return null;
        }
    }

    /**
     * 获取配置文件中的所有权限配置
     *
     * @return
     */
    public static List<PermissionsEntity> getAllPermissions() {
        try {
            List<PermissionsEntity> roles = PermissionsCore.getInstance().getAllPermissions();
            return roles;
        } catch (Exception e) {
            if (log.isErrorEnabled()) {
                log.error("获取所有的角色出现错误!", e);
            }
            return null;
        }
    }

    /**
     * 判断用户是否有相关的角色
     *
     * @param role
     *
     * @return
     */
    public static boolean isUserHaveRole(String role) {
        Subject currentUser = SecurityUtils.getSubject();
        return currentUser.hasRole(role);
    }

    /**
     * 判断用户是否有相关的权限
     *
     * @param permissCodes
     *
     * @return
     */
    public static boolean isUserHavePermiss(String permissCodes) {
        Subject currentUser = SecurityUtils.getSubject();
        return currentUser.isPermitted(permissCodes);
    }
}
